<?php
class Exchange extends Integral{
	
	
	
	
	public function Exchange($member_id){

	  $this->dao = 'Exchange';
		
	}
	
	
	
	//积分互转
	/*
	   
	  $mid 给予积分的人
	  $toid 接收积分的人
	  
	*/
	public function adjustIntegral($mid,$toid,$num){
	   $integral_numberDB = M('integral_number');
	   $mgnumber = $integral_numberDB->where(array('typename'=>'Exchange','member_id'=>$mid))->find();
	   $msnumber = $integral_numberDB->where(array('typename'=>'Exchange','member_id'=>$toid))->find();
	   if( $mgnumber['number'] < $num){
		   
		   return false;
	   }
	   $mgnum = $mgnumber['number']-$num;
	   $gmes = $integral_numberDB->where(array('typename'=>'Exchange','member_id'=>$mid))->save(array('number'=>$mgnum));
	   $msnum = $msnumber['number']+$num;
	   $smes = $integral_numberDB->where(array('typename'=>'Exchange','member_id'=>$toid))->save(array('number'=>$msnum));
	   if($smes && $gmes){
		   
		 $remark = '积分互转，'.$mid.'转:'.$num.'积分:'.$toid;
		 parent::integral_log($mid,$remark,$num);
		  
	     return true;
	   }
	   
	   return false;
	}
	
	
	//资金互转
	/*
	   
	  $mid 给予资金的人
	  $toid 接收资金的人
	  
	*/
	public function adjustMoney($mid,$toid,$num){
	   
	   $memberDB = M('member');
	   $mgnumber = $memberDB->where(array('id'=>$mid))->find();
	   $msnumber = $memberDB->where(array('id'=>$toid))->find();
	   if( $mgnumber['money'] < $num){
		   
		   return false;
	   }
	   $mgnum = $mgnumber['money']-$num;
	   $gmes = $memberDB->where(array('id'=>$mid))->save(array('money'=>$mgnum));
	   $msnum = $msnumber['money']+$num;
	   $smes = $memberDB->where(array('id'=>$toid))->save(array('money'=>$msnum));
	   if($smes && $gmes){
		   
		 $member_account_logDB = M('member_account_log');
		 $remark = '资金互转，'.$mid.'转:'.$num.'资金:'.$toid;
		 $data = array(
		   'member_id'=>$mid,
		   'remark'=>$remark,
		   'dataline'=>time(),
		   'user_id'=>0,
		   'changenum'=>$num,
		 );
		 $member_account_logDB->add($data);
	     return true;
	   }
	   
	   return false;
	}
	
    //判断是否可以互转
	public function judgeIntegral(){
	    return true;
	}



}